﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data;

namespace Koumi.Tools
{
    /// <summary>
    /// Json操作类
    /// </summary>
    public class JsonHelper
    {
        /// <summary>
        /// IList对象转换Json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonName"></param>
        /// <param name="IL"></param>
        /// <returns></returns>
        public static string ObjectToJson<T>(string jsonName, IList<T> IL)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("{'" + jsonName + "':[");
            if (IL.Count > 0)
            {
                for (int i = 0; i < IL.Count; i++)
                {
                    T obj = Activator.CreateInstance<T>();
                    Type type = obj.GetType();
                    PropertyInfo[] pis = type.GetProperties();
                    Json.Append("{");
                    for (int j = 0; j < pis.Length; j++)
                    {
                        Json.Append("'" + pis[j].Name.ToString() + "':'" + pis[j].GetValue(IL[i], null) + "'");
                        if (j < pis.Length - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < IL.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]}");
            return Json.ToString();
        }
        /// <summary>
        /// IList对象转换Json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonName"></param>
        /// <param name="IL"></param>
        /// <param name="columnFeild">要加入Json对象的字段</param>
        /// <returns></returns>
        public static string ObjectToJson<T>(string jsonName, IList<T> IL,string[] columnFeild)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("{'" + jsonName + "':[");
            if (IL.Count > 0)
            {
                for (int i = 0; i < IL.Count; i++)
                {
                    T obj = Activator.CreateInstance<T>();
                    Type type = obj.GetType();
                    PropertyInfo[] pis = type.GetProperties(); 
                    Json.Append("{");
                    for (int j = 0; j < pis.Length; j++)
                    {
                        foreach (string s in columnFeild)
                        {
                            if (pis[j].Name.ToString() == s)
                            {
                                Json.Append("'" + pis[j].Name.ToString() + "':'" + pis[j].GetValue(IL[i], null) + "'");
                                if (j < pis.Length - 1)
                                {
                                    Json.Append(",");
                                }
                                break;
                            }
                        } 
                    }
                    Json.Append("}");
                    if (i < IL.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]}");
            return Json.ToString();
        }

        /// <summary>
        /// DataTable转成Json
        /// </summary>
        /// <param name="jsonName"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DataTableToJson(string jsonName, DataTable dt)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("{\"" + jsonName + "\":[");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Json.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
                        if (j < dt.Columns.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < dt.Rows.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]}");
            return Json.ToString();
        } 
    }
}
